package com.walkline.screen;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.FlowFieldManager;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.walkline.util.Emotions;
import com.walkline.util.ui.MyEmtionBitmapField;

public class ShowEmotionPopupScreen extends PopupScreen
{
	//private FlowFieldManager _foreground = new FlowFieldManager(VERTICAL_SCROLL | USE_ALL_HEIGHT | Manager.FIELD_HCENTER);
	private FlowFieldManager[] _foreground;
	private MyEmtionBitmapField[][] _emotionFields;
	private Emotions _emotions = Emotions.getInstance();
	private String _emotion = "";
	private boolean _done = false;
	private LabelField _titleField = new LabelField("", Field.FIELD_HCENTER);
	//private LabelField _titleCount = new LabelField("", Field.FIELD_RIGHT);
	private int _currentGroup = 0;

	public ShowEmotionPopupScreen()
	{
		super(new VerticalFieldManager(FOCUSABLE | NO_VERTICAL_SCROLL));

		//HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH);
		VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_WIDTH);
		vfm.setMargin(0, 0, 10, 0);
		vfm.add(_titleField);
		//hfm.add(_titleCount);
		//vfm.add(hfm);
		add(vfm);

		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run() {loadEmotions();}
		});
	}

	private void switchEmotions()
	{
		final int last_group = _currentGroup;

		_currentGroup += 1;
		if (_currentGroup == _emotions.getGroupCount()) {_currentGroup = 0;}

		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run()
			{
				delete(_foreground[last_group]);
				add(_foreground[_currentGroup]);

				//_titleCount.setText((_currentGroup + 1) + " / " + _emotions.getGroupCount());
			}
		});
	}

	private void loadEmotions()
	{
		int group_count = _emotions.getGroupCount();
		int emotion_count = 0;

		_foreground = new FlowFieldManager[group_count];
		_emotionFields = new MyEmtionBitmapField[group_count][];

		for (int i=0; i<group_count; i++)
		{
			_foreground[i] = new FlowFieldManager(VERTICAL_SCROLL | USE_ALL_HEIGHT | Manager.FIELD_HCENTER);
			emotion_count = _emotions.getCount(i);
			_emotionFields[i] = new MyEmtionBitmapField[emotion_count];

			for (int j=0; j<emotion_count; j++)
			{
				_emotionFields[i][j] = new MyEmtionBitmapField(_emotions.getEmotionsArray(i, j));					
				_emotionFields[i][j].setChangeListener(new FieldChangeListener()
				{
					public void fieldChanged(Field field, int context)
					{
						if (context == 2)
						{
							_done = false;
							_titleField.setText(field.getCookie());
						} else if (context == 0) {
							_done = true;
							_emotion = (String) field.getCookie();
							onClose();						
						}
					}
				});

				_foreground[i].add(_emotionFields[i][j]);
			}
		}

		add(_foreground[_currentGroup]);

		//_titleCount.setText((_currentGroup + 1) + " / " + group_count);
	}

	public String getEmotion()
	{
		String returnValue = null;

		if (_done)
		{
			_done = false;

			returnValue = _emotion;
		}

		return returnValue;
	}

	protected boolean keyDown(int keycode, int time)
	{
		switch (keycode)
		{
			case 8323072: //sym key pressed
				switchEmotions();
				return true;
		}

		return super.keyDown(keycode, time);
	}

	public boolean onClose()
	{
		UiApplication.getUiApplication().popScreen(this);
		return true;
	}
}